﻿#include <iostream>
#include <fstream>
#include <thread>
#include <mutex>
#include <atomic>
#include <vector>
using namespace std;

int value=0;

ofstream outfiletxt("hashkey.txt");

mutex mut;

vector<int> numtest;

void fun(int i) 
{
	unique_lock <mutex> ulk(mut, defer_lock);
	int a = 0;
	while (true)
	{
		ulk.lock();
		numtest[value];
		a= numtest[value];
		value++;
		ulk.unlock();

		this_thread::sleep_for(chrono::seconds(1));

		ulk.lock();
		outfiletxt << "thread: " << i << "; " << a << endl;
		ulk.unlock();

		if (value >= numtest.size()-1)
		{
			return;
		}
	}
}


int main() 
{
	for (int i = 0; i < 100; i++)
	{
		numtest.push_back(i);
	}

	vector<thread> threadVector;
	for (int i = 0; i < 6; i++)
	{
		threadVector.push_back(thread(fun,i));
	}
	for (int i = 0; i < 6; i++)
	{
		threadVector[i].join();
	}

	return 0;
}